package com.bjpowernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.entity.PmsBrand;
import com.bjpowernode.entity.UmsAdmin;
import com.bjpowernode.feign.FileService;
import com.bjpowernode.mapper.PmsBrandMapper;
import com.bjpowernode.service.PmsBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2024-05-22
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {

    @Resource
    PmsBrandMapper pmsBrandMapper;
    @Resource
    FileService fileService;
    @Override
    public IPage<PmsBrand> getpmsBrandList(Integer pageNo, Integer pageSize, String name) {
        QueryWrapper<PmsBrand> wrapper = new QueryWrapper<>();
        wrapper.like("name",name.trim());
        wrapper.orderByDesc("sort");
        return this.page(new Page<>(pageNo, pageSize), wrapper);
    }

    @Override
    public Boolean save(String name, String description, MultipartFile file) {
        String logo = fileService.imageUpload(file);
        PmsBrand pmsBrand = new PmsBrand(name, description);
        pmsBrand.setLogo(logo);
        return this.save(pmsBrand);
    }

    @Override
    public Boolean update(String id, String name, String description, MultipartFile file) {
        PmsBrand pmsBrand = new PmsBrand(id, name, description);
        if (file != null) {
            pmsBrand.setLogo(fileService.imageUpload(file));
        }
        return this.updateById(pmsBrand);
    }

    @Override
    public Boolean delete(String id, Boolean active) {
        return this.updateById(new PmsBrand(id,active));
    }

    @Override
    public List<PmsBrand> getActive() {
        QueryWrapper<PmsBrand> wrapper = new QueryWrapper<>();
        wrapper.eq("active",1);
        return this.list(wrapper);
    }

    @Override
    public String getNameById(String brandId) {
        return this.getById(brandId).getName();
    }
}
